import { defineConfig } from "vite";
import path from "path";
import vue from "@vitejs/plugin-vue";
//  引用mock
import { viteMockServe } from "vite-plugin-mock";
import { loadEnv } from "./src/utils/viteBuild";

const resolve = (dir) => path.join(__dirname, dir);
const { VITE_PORT, VITE_OPEN, VITE_PUBLIC_PATH } = loadEnv();

// https://vitejs.dev/config/
export default defineConfig({
  base: process.env.NODE_ENV === "production" ? VITE_PUBLIC_PATH : "./",
  root: process.cwd(),
  resolve: {
    alias: {
      "@": resolve("/src/"),
    },
  },
  plugins: [
    vue(),
    viteMockServe({
      supportTs: false,
    }),
  ],
  server: {
    host: "0.0.0.0",
    port: VITE_PORT,
    open: VITE_OPEN,
    proxy: {
      "/api": {
        target: `http://localhost:8000`,
        ws: true,
        changeOrigin: true,
      },
      "/mock": {
        target: `http://localhost:${VITE_PORT}`,
        ws: true,
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/mock/, ""),
      },
    },
  },
  build: {
    outDir: "dist",
    minify: "esbuild",
    sourcemap: false,
  },
});
